package com.topfinance.payment.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.topfinance.frame.dao.HibernateEntityDao;
import com.topfinance.frame.exception.AppException;
import com.topfinance.frame.util.StringUtil;
import com.topfinance.payment.ebo.TC2BisTpMsgEbo;
import com.topfinance.payment.service.ITC2BisTpMsgMgr;

/**
 * 该类自动实现了dao的一般方法，如add, remove, update
 * 在service中出现的大部分情况下是在向后台提交数据前的处理(如校验)及获取后台数据后的处理等
 * @author
 *
 */

public class TC2BisTpMsgMgrImpl extends HibernateEntityDao<TC2BisTpMsgEbo> implements ITC2BisTpMsgMgr {

	private static final Log log = LogFactory.getLog(TC2BisTpMsgMgrImpl.class);
	
	//获得业务种类与报文对应	
	public TC2BisTpMsgEbo getTC2BisTpMsg(Integer oid) throws AppException{
		return (TC2BisTpMsgEbo)get(oid);
	}

	public void deleteTC2BisTpMsg(Integer oid) throws AppException{
		removeById(TC2BisTpMsgEbo.class,oid);
	}
	
	//返回TC2BisTpMsgEbo
	//参数：msgcode=消息编号，tpcode=业务类型
	public TC2BisTpMsgEbo getBisTpMsgEbo(String msgcode,String tpcode) throws AppException{
		if(StringUtil.isNullStr(msgcode)||StringUtil.isNullStr(tpcode))
			throw new AppException("[调用参数都必须设置]");
		String hql = " from TC2BisTpMsgEbo t " ;
		String where = " where t.msgCode = '" + msgcode + "'";
		where += " and t.tpCode = '" + tpcode + "'";
		List<Object> paramList = new ArrayList<Object>();
		hql = hql + where;
		List<TC2BisTpMsgEbo> tps = find(hql,paramList.toArray());
		if(tps.size() > 1)
			throw new AppException("[对应的业务类型对象超过一个]msgcode=" + msgcode + ",tpcode=" + tpcode);
		else if(tps.size() == 1)
			return tps.get(0);
		else
			return null;
	}

	//获得业务属性列表
	public List<TC2BisTpMsgEbo> getAllBisTpMsg() throws AppException{
		String hql = " from TC2BisTpMsgEbo t " ;
		String where = "";
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where;
		}
		hql = hql + where;
		return find(hql,paramList.toArray());		
	}
	
	//获得业务类型报文对应
	//输入A100，01300,HVPS
	//返回HVPS.111.001.01
	public List<TC2BisTpMsgEbo> getAllBisTpMsg(String tpCode,String clasCode,String appType) throws AppException{
		//select btm.msg_code,btm.msg_name from T_C2_BIS_TP_MSG btm,t_c2_bis_clas bc 
		//where btm.tp_code = bc.tp_code and bc.cls_code =  '01300' and btm.tp_code = 'A101' and bc.app_type = 'BEPS'
		String hql = " select btm from TC2BisTpMsgEbo btm, TC2BisClasEbo bc" ;
		String where = " where btm.tpCode = bc.TC2BisType.tpCode and bc.clsCode = '" + clasCode + "'";
		where += " and btm.isAcitve = 'Y'";
		where += " and btm.tpCode = '" + tpCode + "'";
		where += " and bc.appType = '" + appType + "'";
		where += " and btm.appType = '" + appType + "'";
		return find(hql+where);
	}

	//获得业务报文对应的业务种类
	//输入HVPS.111.001.01
	//返回
	public List<TC2BisTpMsgEbo> getAllBisTpMsg(String msgCode) throws AppException{
		String hql = " from TC2BisTpMsgEbo btm " ;
		String where = " where btm.msgCode = '" + msgCode + "' and btm.isAcitve = 'Y' order by btm.msgCode";
		return find(hql+where);	
	}

	//获得顶级报文列表
	public List getTopBisMsg() throws AppException{
		String hql = " select distinct btm.msgCode,btm.msgName from TC2BisTpMsgEbo btm " ;
		String where = " where btm.isAcitve = 'Y' order by btm.msgCode";
		return find(hql+where);		
	}
	
	//获得业务类型报文对应,此方法只有在tpcode不对应clascode情况调用
	//参数：tpCode=类型，appType=应用模块
	public TC2BisTpMsgEbo getBisTpMsgEboWithNoMsg(String tpCode,String appType) throws AppException{
		if(StringUtil.isNullStr(tpCode))
			throw new AppException("[参数'业务类型'不能为空]");
		String hql = " from TC2BisTpMsgEbo btm " ;
		String where = " where btm.tpCode = '" + tpCode + "'";
		if(StringUtil.isNotNullStr(appType))
			where += " and btm.appType = '" + appType + "'";
		where += " and btm.isAcitve = 'Y' order by btm.msgCode";
		List<TC2BisTpMsgEbo> tps = find(hql+where);
		if(tps.size() !=1)
			throw new AppException("[无法找到对应的TC2BisTpMsgEbo对象]tpCode="+tpCode);
		else
			return tps.get(0);
	}
}
